home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / indents.zip / comment.c < prev    next >
C/C++ Source or Header  |  1993-05-30  |  18KB  |  412 lines

  1. /**
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted provided
  8.  * that the above copyright notice and this paragraph are duplicated in all
  9.  * such forms and that any documentation, advertising materials, and other
  10.  * materials related to such distribution and use acknowledge that the
  11.  * software was developed by the University of California, Berkeley, the
  12.  * University of Illinois, Urbana, and Sun Microsystems, Inc.  The name of
  13.  * either University or Sun Microsystems may not be used to endorse or
  14.  * promote products derived from this software without specific prior written
  15.  * permission. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18.  */
  19.  
  20. #include "globals.h"
  21.  
  22. #ifndef lint
  23. # ifndef ANSIC
  24. static char     sccsid[] = "@(#)comment.c    6.0 (Berkeley) 92/06/15";
  25. # endif         /* ANSIC */
  26. #endif          /* not lint */
  27.  
  28.  
  29. /**
  30.  * NAME: pr_comment
  31.  *
  32.  * FUNCTION: This routine takes care of scanning and printing comments.
  33.  *
  34.  * ALGORITHM: 1) Decide where the comment should be aligned, and if lines should
  35.  * be broken. 2) If lines should not be broken and filled, just copy up to
  36.  * end of comment. 3) If lines should be filled, then scan through input_buffer
  37.  * copying characters to com_buf.  Remember where the last blank, tab, or
  38.  * new line was.  When line is filled, print up to last blank and continue
  39.  * copying.
  40.  *
  41.  * HISTORY: November 1976, D A Willcox of CAC, Initial coding.
  42.  * 12/6/76, D A Willcox of CAC, Modification to handle UNIX-style comments.
  43.  */
  44.  
  45.  
  46. /* this routine processes comments.  It makes an attempt to keep comments
  47.  * from going over the max. line length.  If a line is too long, it moves
  48.  * everything from the last blank to the next comment line.  Blanks and tabs
  49.  * from the beginning of the input line are removed */
  50.  
  51.  
  52. #ifdef ANSIC
  53. void            pr_comment(char *combuf)
  54. #else           /* ANSIC */
  55. pr_comment()
  56.     char           *combuf;
  57. #endif          /* ANSIC */
  58. {
  59.     int             now_col;    /* column we are in now */
  60.     int             adj_max_col;/* Adjusted max_col for when we decide to
  61.                                  * spill comments over the right margin */
  62.     char           *last_bl = NULL; /* points to the last blank in the output
  63.                                      * buffer */
  64.     char           *t_ptr = NULL;   /* used for moving string */
  65.     int             unix_comment;   /* tri-state variable used to decide if
  66.                                      * it is a UNIX-style comment. 0 means
  67.                                      * only blanks since '/''*', 1 means
  68.                                      * regular style comment, 2 means UNIX
  69.                                      * style comment */
  70.     int             break_delim = comment_delimiter_on_blankline;
  71.     int             l_just_saw_decl = ps.just_saw_decl;
  72.  
  73. #if 0
  74.     int             ps.last_nl = 0; /* true iff the last significant thing
  75.                                      * we've seen is a newline */
  76. #endif          /* 0 */
  77.     int             one_liner = 1;  /* true iff this comment is a one-liner */
  78.  
  79.     adj_max_col = max_col;
  80.     ps.just_saw_decl = 0;
  81.     last_bl = 0;                /* no blanks found so far */
  82.     ps.box_com = false;         /* at first, assume that we are not in a
  83.                                  * boxed comment or some other comment that
  84.                                  * should not be touched */
  85.     ++ps.out_coms;              /* keep track of number of comments */
  86.     unix_comment = 1;           /* set flag to let us figure out if there is
  87.                                  * a unix-style comment ** DISABLED: use 0 to
  88.                                  * re-enable this hack! */
  89.  
  90.     /* Figure where to align and how to treat the comment */
  91.  
  92.     if (ps.col_1 && !format_col1_comments) {    /* if comment starts in
  93.                                                  * column 1 it should not be
  94.                                                  * touched */
  95.         ps.box_com = true;
  96.         ps.com_col = 1;
  97.     } else {
  98.         if (*buf_ptr == '-' || *buf_ptr == '*') {
  99.             ps.box_com = true;  /* a comment with a '-' or '*' immediately
  100.                                  * after the '/''*' is assumed to be a boxed
  101.                                  * comment */
  102.             break_delim = 0;
  103.         }
  104.         if ( /* ps.bl_line && */ (s_lab == e_lab) && (s_code == e_code)) {
  105.             /* klg: check only if this line is blank */
  106.             /* If this (*and previous lines are*) blank, don't put comment
  107.              * way out at left */
  108.             ps.com_col = (ps.ind_level - ps.unindent_displace) * ps.ind_size + 1;
  109.             adj_max_col = block_comment_max_col;
  110.             if (ps.com_col <= 1)
  111.                 ps.com_col = 1 + !format_col1_comments;
  112.         } else {
  113.             register int    target_col;
  114.  
  115.             break_delim = 0;
  116.             if (s_code != e_code)
  117.                 target_col = count_spaces(compute_code_target(), s_code);
  118.             else {
  119.                 target_col = 1;
  120.                 if (s_lab != e_lab)
  121.                     target_col = count_spaces(compute_label_target(), s_lab);
  122.             }
  123.             ps.com_col = ps.decl_on_line || ps.ind_level == 0 ? ps.decl_com_ind : ps.com_ind;
  124.             if (ps.com_col < target_col)
  125.                 ps.com_col = target_col +
  126.                     (tabsize - ((target_col - 1) % tabsize));   /* JHT 22oct89 */
  127.             if (else_or_endif) {/* -cp PETER */
  128.                 ps.com_col = ps.else_endif_col; /* -cp PETER */
  129.                 else_or_endif = false;  /* -cp PETER */
  130.                 /* We want the comment to appear one space after the #else or
  131.                  * #endif.                         -cp PETER */
  132.                 if (ps.com_col < target_col)    /* -cp PETER */
  133.                     ps.com_col = target_col + 1;    /* -cp PETER */
  134.             }                   /* -cp PETER */
  135.             if (ps.com_col + 24 > adj_max_col)
  136.                 adj_max_col = ps.com_col + 24;
  137.         }
  138.     }
  139.     if (ps.box_com) {
  140.         buf_ptr[-2] = 0;
  141.         ps.n_comment_delta = 1 - count_spaces(1, in_buffer);
  142.         buf_ptr[-2] = '/';
  143.     } else {
  144.         ps.n_comment_delta = 0;
  145.         while (*buf_ptr == ' ' || *buf_ptr == '\t')
  146.             buf_ptr++;
  147.     }
  148.     ps.comment_delta = 0;
  149.     *e_com++ = '/';             /* put '/''*' into buffer */
  150.     if (ps.cc_comment)          /* (or '/''/') */
  151.         *e_com++ = '/';
  152.     else
  153.         *e_com++ = '*';
  154.     if (*buf_ptr != ' ' && !ps.box_com)
  155.         *e_com++ = ' ';
  156.  
  157.     *e_com = '\0';
  158.     if (troff) {
  159.         now_col = 1;
  160.         adj_max_col = 80;
  161.     } else
  162.         now_col = count_spaces(ps.com_col, s_com);  /* figure what column we
  163.                                                      * would be in if we
  164.                                                      * printed the comment
  165.                                                      * now */
  166.  
  167.     /* Start to copy the comment */
  168.  
  169.     while (1) {                 /* this loop will go until the comment is
  170.                                  * copied */
  171.         if (*buf_ptr >= 040 && *buf_ptr != '*')
  172.             ps.last_nl = 0;
  173.         check_size(com);
  174.         switch (*buf_ptr) {     /* this checks for various spcl cases */
  175.         case 014:               /* check for a form feed */
  176.             if (!ps.box_com) {  /* in a text comment, break the line here */
  177.                 ps.use_ff = true;
  178.                 /* fix so dump_line uses a form feed */
  179.                 dump_line();
  180.                 last_bl = 0;
  181.                 *e_com++ = ' ';
  182.                 *e_com++ = '*';
  183.                 *e_com++ = ' ';
  184.                 while (*++buf_ptr == ' ' || *buf_ptr